﻿using System;
using System.Collections.Generic;
using DataAccessLibrary.DAL;
using entities;

namespace BusinessLogicLibrary
{
    public class PromotionModule
    {
        private readonly PromotionDAO _promotionDAO = new PromotionDAO();

        public List<vSubPromotion> GetPromotion(bool available)
        {
            return available ? _promotionDAO.SelectPromotion(1) : _promotionDAO.SelectPromotion(-1);
        }

        public Promotion GetPromotion(int id)
        {
            return _promotionDAO.SelectPromotionById(id);
        }

        public List<vSubPromotion> GetPromotion(DateTime begin, DateTime end)
        {
            return _promotionDAO.SelectPromotionByTime(begin, end);
        }

        public List<vSubPromotion> GetPromotion(string title)
        {
            return _promotionDAO.SelectPromotionByTitle(title);
        }

        public bool AddPromotion(DateTime startTime, DateTime endTime, string title, string desciption, byte[] banner)
        {
            if (title.Trim() != "")
            {
                if (desciption.Trim() != "")
                {
                    var promotion = new Promotion
                                        {
                                            PromotionEnd = endTime,
                                            PromotionBanner = banner,
                                            PromotionTitle = title,
                                            PromotionDescription = desciption,
                                            PromotionStart = startTime
                                        };
                    return _promotionDAO.InsertPromotion(promotion);
                }
                throw new Exception("Desciption is empty");
            }
            throw new Exception("Promotion Title is empty");
        }

        public bool ModifyPromotion(int promotionId, DateTime startTime, DateTime endTime, string title,
                                    string desciption, byte[] banner)
        {
            if (title.Trim() != "")
            {
                if (desciption.Trim() != "")
                {
                    var promotion = new Promotion
                                        {
                                            PromotionEnd = endTime,
                                            PromotionStart = startTime,
                                            PromotionBanner = banner,
                                            PromotionTitle = title,
                                            PromotionDescription = desciption,
                                            PromotionID = promotionId
                                        };
                    return _promotionDAO.UpdatePromotion(promotion);
                }
                throw new Exception("Desciption is empty");
            }
            throw new Exception("Promotion Title is empty");
        }

        public bool removePromotion(int promotionId)
        {
            return _promotionDAO.DeletePromotion(promotionId);
        }

        public List<vSubPromotionDetail> GetPromotionDetailByPromotionId(int promotionId)
        {
            return _promotionDAO.SelectPromotionDetailByPromotionId(promotionId);
        }

        public PromotionDetail GetPromotionDetail(int promotionDetailId)
        {
            return _promotionDAO.SelectPromotionDetail(promotionDetailId);
        }

        public bool AddPromotionDetail(int promotionId, string promotionContent, byte[] promotionImage)
        {
            if (promotionContent.Trim() != "")
            {
                return _promotionDAO.InsertPromotionDetail(promotionId, promotionContent, promotionImage);
            }
            throw new Exception("Promotion Content is empty");
        }

        public bool ModifyPromotionDetail(int promotionDetailId, string promotionContent, byte[] promotionImage)
        {
            if (promotionContent.Trim() != "")
            {
                return _promotionDAO.UpdatePromotionDetail(promotionDetailId, promotionContent, promotionImage);
            }
            throw new Exception("Promotion Content is empty");
        }

        public bool RemovePromotionDetail(int promotionDetailId)
        {
            return _promotionDAO.DeletePromotionDetail(promotionDetailId);
        }
    }
}